package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.bac.BacAntClassDto;
import com.ysd.lis.entity.bac.*;
import com.ysd.lis.mapper.bac.BacNaturalMdrRuleMapper;
import com.ysd.lis.request.bac.BacAntClassParam;
import com.ysd.lis.service.bac.BacNaturalMdrRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-01-25
 */
@Service
@Slf4j
public class BacNaturalMdrRuleServiceImpl extends ServiceImpl<BacNaturalMdrRuleMapper, BacNaturalMdrRule> implements BacNaturalMdrRuleService {

    @Autowired
    BacNaturalMdrRuleMapper bacNaturalMdrRuleMapper;

    @Override
    public Result findBacNaturalMdrRuleList(BacAntClassParam param) {
        MPJLambdaWrapper<BacNaturalMdrRule> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacNaturalMdrRule.class)
                .selectAs(BacAntClass::getAntClassNa, BacNaturalMdrRule::getAntClassNa)
                .selectAs(BacGermDict::getGermNa, BacNaturalMdrRule::getGermNa)
                .selectAs(BacAntSubClass::getSubClassNa, BacNaturalMdrRule::getSubClassNa)
                .selectAs(BacAntibioticsDict::getAntName, BacNaturalMdrRule::getAntNa)

                .leftJoin(BacAntClass.class, "sa", p -> p.eq(BacAntClass::getAntClassNo, BacNaturalMdrRule::getAntClassNo).eq(BacAntClass::getDelFlag, 0))
                .leftJoin(BacGermDict.class, "bg", p -> p.eq(BacGermDict::getGermNo, BacNaturalMdrRule::getGermNo).eq(BacGermDict::getDelFlag, 0))
                .leftJoin(BacAntSubClass.class, "su", p -> p.eq(BacAntSubClass::getSubClassNo, BacNaturalMdrRule::getSubClassNo).eq(BacAntSubClass::getDelFlag, 0))
                .leftJoin(BacAntibioticsDict.class, "bd", p -> p.eq(BacAntibioticsDict::getAntCode, BacNaturalMdrRule::getAntNo).eq(BacAntibioticsDict::getDelFlag, 0));



        queryWrapper.eq(BacNaturalMdrRule::getDelFlag, 0)
                .orderByAsc(BacNaturalMdrRule::getCreateTime);
        List<BacNaturalMdrRule> ruleList = bacNaturalMdrRuleMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", ruleList);
    }

    @Override
    public Result saveOrUpdateBacNaturalMdrRule(BacAntClassDto dto) {
        List<BacNaturalMdrRule> bacNaturalMdrRuleList = dto.getBacNaturalMdrRuleList();
        boolean b = this.saveOrUpdateBatch(bacNaturalMdrRuleList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteBacNaturalMdrRule(BacAntClassDto dto) {
        List<BacNaturalMdrRule> bacNaturalMdrRuleList = dto.getBacNaturalMdrRuleList();
        List<String> ids = bacNaturalMdrRuleList.stream().map(BacNaturalMdrRule::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
